﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Journal.Admin.Presentation.IPresentationServices;
using Journal.Admin.Presentation.Mappers;
using Journal.Admin.Presentation.Models.UserModels;
using Journal.Common.Objects.Enums;
using Journal.Domain.IDomainServices;
using Journal.Domain.Models;

namespace Journal.Admin.Presentation.PresentationServices
{
    public class UserPresentationService : IUserPresentationService
    {
        private readonly IUserDomainService userDomainService;
        private readonly IRoleDomainService roleDomainService;

        public UserPresentationService(IUserDomainService userDomainService, IRoleDomainService roleDomainService)
        {
            this.userDomainService = userDomainService;
            this.roleDomainService = roleDomainService;
        }

        public UserSelectModel GetSelectModel()
        {
            IEnumerable<User> users = userDomainService.GetAll();
            return new UserSelectModel(users);
        }

        public UserEditModel Save(UserEditModel model)
        {
            User user = userDomainService.Get(model.Id);
            model.MapTo(user);
            userDomainService.Add(user);
            return new UserEditModel(user);
        }

        public UserEditModel CreateEditModel()
        {
            UserEditModel model = new UserEditModel();
            FillEditModel(model);
            return model;
        }

        public UserEditModel GetUserById(int id)
        {
            User user = userDomainService.Get(id);
            UserEditModel model = new UserEditModel(user);
            FillEditModel(model);
            return model;
        }

        public UserEditModel GetUserByLogin(string login)
        {
            User user = userDomainService.GetUserByLogin(login);
            UserEditModel model = new UserEditModel(user);
            FillEditModel(model);
            return model;
        }

        public UserLoginModel UserLogOn(string login)
        {
            User user = userDomainService.GetUserByLogin(login);
            UserLoginModel model = new UserLoginModel(user);
            return model;
        }

        public string GetUserHomeSection(int id)
        {
            User user = userDomainService.Get(id);
            return user.Permissions.First().Name;
        }

        public void Delete(int id)
        {
            var model = userDomainService.Get(id);
            userDomainService.Delete(model);
        }

        public void FillEditModel(UserEditModel model)
        {
            IList<Role> roles = roleDomainService.GetAll();
            model.Roles = new List<SelectListItem>(roles.Select(x => new SelectListItem() { Text = x.Name, Value = x.Id.ToString() }));
            model.GenderTypeses = new List<SelectListItem>() { 
                                                                new SelectListItem() { Text = GenderTypes.Male.ToString(), Value = ((int)GenderTypes.Male).ToString()},
                                                                new SelectListItem() { Text = GenderTypes.Women.ToString(), Value = ((int)GenderTypes.Women).ToString()}
                                                             };
        }
    }
}